import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/widgets/card.dart';
import '../resources/colours.dart';
import 'ladder_tab.dart';

class TabStyle{
  TabStyle({this.color,this.size,this.weight});

  double? size;
  Color? color;
  FontWeight? weight;

}

class LadderTabBar extends StatelessWidget{
  LadderTabBar({
    super.key,
    this.onTap,
    required this.tabs,
    this.tag,
    this.selectLabelStyle,
    this.labelStyle,
    this.needStart = true,
    this.needEnd = true,
    this.scrollable = false,
    this.labelPadding,
    this.keepSize = true,
    this.initialIndex = 0
  });

  final ValueChanged<int>? onTap;
  final String? tag;
  final TabStyle? selectLabelStyle;
  final TabStyle? labelStyle;
  final List<Widget> tabs;
  final bool scrollable;
  final bool needStart;
  final bool needEnd;
  final EdgeInsetsGeometry? labelPadding;
  final bool keepSize;
  final int initialIndex;
  late final TabBlockController controller;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<TabBlockController>(
      autoRemove: false,
      initState: (state) {
        controller = Get.put(TabBlockController(),tag: tag);
        state.controller?.index = initialIndex;
      },
      tag: tag,
      builder: (controller) {
        var child = Row(
          children: List.generate(tabs.length, (index) {
            TabPlace place = TabPlace.middle;
            if(index == 0 && needStart){
              place = TabPlace.start;
            }else if(index == tabs.length-1 && needEnd){
              place = TabPlace.end;
            }
            var tab = GestureDetector(
              onTap: () {
                controller.index = index;
                onTap?.call(controller.index);
              },
              child: DefaultTextStyle(
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: index == controller.index
                      ? (selectLabelStyle?.weight ?? FontWeight.w600)
                      : (labelStyle?.weight ?? FontWeight.w400),
                    color: controller.index == index
                      ? (selectLabelStyle?.color ?? Colours.text)
                      : (labelStyle?.color ?? Colours.grey66)
                  ),
                  child: LadderTab(
                      place: place,
                      height: 34,
                      scrollable: scrollable,
                      keepSize: keepSize,
                      color: index == controller.index
                          ? Colours.white
                          : Colours.transparent,
                      child: Padding(
                        padding: labelPadding ?? EdgeInsets.zero,
                        child: tabs[index],
                      )
                  )
              ),
            );
            return scrollable?tab:Expanded(child: tab);
          })
        );
        return scrollable?
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: child):
          child;
      }
    );
  }
}

